package myJs.cps

import japgolly.scalajs.react._
import japgolly.scalajs.react.vdom.all._
import myJs.Implicits._
import myJs.myPkg.DatepickerOptions
import myJs.myPkg.jquery._
import myJs.tool.Tool
import org.scalajs.dom.{Event, html}
import org.scalajs.dom.html.Input
import myJs.myPkg.Hooks._

import scala.collection.immutable.SeqMap
import scala.scalajs.js

/** Created by yz on 15/9/2023
  */
object PureSelectCp {

  case class Props(
      name: String = "",
      optionMap: SeqMap[String, String],
      hasBv: Boolean = false,
      formJq: JQuery = $("#form"),
      value: String,
      onChange: String => Callback,
      selfClass: String = "",
      width: String = "100%",
    disabled: Boolean = false,
  ){
    @inline def render: VdomElement = {
      Component(this)
    }
  }

  val Component = ScalaFnComponent[Props] { props =>
    val selectRef = Ref[html.Select]

    def valueChange(e: ReactEventFromInput) = {
      val v = e.target.value
      props.onChange(v)
    }

    select(
      width := props.width,
      className := s"form-control ${props.selfClass}",
      name := s"${props.name}",
      value := s"${props.value}",
      onChange ==> valueChange,
      disabled:=props.disabled,
      props.optionMap.toVdomArray { case (v, text) =>
        option(key := s"${v}", value := s"${v}", s"${text}")
      }
    ).withRef(selectRef)

  }

}
